import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:transparent_image/transparent_image.dart';
import 'package:iea/provider/base/base_resp.dart';
import 'package:iea/blocs/indexPage_blocs/index_goodCourse_bloc.dart';
import 'package:iea/models/indexPage_models/index_goodCourse_model.dart';
import 'package:iea/widgets/loading.dart';
class GoodCourse extends StatefulWidget{
  @override 
  State<StatefulWidget> createState() => _GoodCourseState();
}
class _GoodCourseState extends State<GoodCourse>
  with AutomaticKeepAliveClientMixin<GoodCourse> {
  @override
  bool get wantKeepAlive => true;
  IndexMenuClassListBloc _bloc = new IndexMenuClassListBloc();
  List<IndexGoodCourseModel> indexMenuClassList = [];
    _getIndexBanner() {
    _bloc.indexMenuClassList();
  }
  @override
  void initState() {
    super.initState();
    _getIndexBanner();
  }
  @override
  Widget build(BuildContext context){
    super.build(context);
    ScreenUtil.instance = ScreenUtil(width: 375, height: 667)..init(context);
    return StreamBuilder<BaseResp>(
      stream: _bloc.indexMenuClassListStream,
      builder: (context, AsyncSnapshot<BaseResp> snapshot) {
        if (snapshot.data != null && snapshot.hasData) {
          var response = snapshot.data;
          if (response.result == true && response.data != null && response.data.length > 0) {
            indexMenuClassList = response.data;
            return Column(
              children: <Widget>[
                Container(
                  width: MediaQuery.of(context).size.width,
                  padding: EdgeInsets.only(left: 17.0),
                  decoration: BoxDecoration(
                    border: Border(top: BorderSide(color: Color.fromRGBO(247, 247, 247, 1), width: 10.0))
                  ),
                  child: Text('IEA认证项目',
                    style: TextStyle(fontSize: 17.0, color: Color.fromRGBO(25, 25, 25, 1), height: 1.5, fontWeight: FontWeight.bold),
                  ),
                ),
                GestureDetector(
                  onTap: (){
                    print('IEA认证项目');
                  },
                  child: indexMenuClassList[0].bannerUrl == null ? Container() : Padding(
                    padding: EdgeInsets.symmetric(vertical: 5.0, horizontal: 17.0),
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(ScreenUtil().setWidth(10)),
                      child: FadeInImage.memoryNetwork(
                        placeholder: kTransparentImage,
                        image: indexMenuClassList[0].bannerUrl,
                        fit: BoxFit.fill,
                        width: MediaQuery.of(context).size.width,
                        height: ScreenUtil().setHeight(118),
                      ),
                    ),
                  ),
                ),
                Container(
                  padding: EdgeInsets.only(top: 10.0),
                  child: ListView.separated(
                    shrinkWrap: true,
                    itemCount: indexMenuClassList.length,
                    padding: EdgeInsets.only(top: 0),
                    physics: ScrollPhysics(),
                    itemBuilder: (BuildContext context, int index){
                      return listItem(indexMenuClassList[index]);
                    },
                    separatorBuilder: (BuildContext context, int index){
                      return Container(
                        height: 1.0,
                        margin: EdgeInsets.symmetric(horizontal: ScreenUtil().setWidth(17)),
                        decoration: BoxDecoration(color: Color.fromRGBO(245, 245, 245, 1)),
                      );
                    }
                  ),
                ),
              ],
            );
          } else {
            return Container();
          }
        } else {
          return LoadingPage();
        }
      }
    );
  }
  // IEA认证项目
  Widget listItem(item){
    return GestureDetector(
      onTap: (){
        print(item);
      },
      child: Container(
        decoration: BoxDecoration(color: null),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Padding(
              padding: EdgeInsets.only(left: 17.0, top: 15.0, bottom: 15.0),
              child: ClipRRect(
                borderRadius: BorderRadius.circular(ScreenUtil().setWidth(10)),
                child: Container(
                  width:  ScreenUtil().setWidth(146),
                  height:  ScreenUtil().setWidth(81),
                  child: FadeInImage.memoryNetwork(
                    placeholder: kTransparentImage,
                    image: item.gdCover != null ? item.gdCover : 'https://xszx-test-1251987637.cos.ap-beijing.myqcloud.com/flutter/images/courseDefault.png',
                    fit: BoxFit.fill,
                  ),
                ),
              ),
            ),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Container(
                  width:  ScreenUtil().setWidth(175),
                  height:  45.0,
                  margin: EdgeInsets.all( ScreenUtil().setWidth(17)),
                  child: Text(item.goName,
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(color: Color.fromRGBO(25, 25, 25, 1), fontSize: 17.0),
                  ),
                ),
                Flex(
                  direction: Axis.horizontal,
                  children: <Widget>[
                    Padding(padding: EdgeInsets.only(left: 15.0),),
                    Text(item.discountPrice == null ? (item.price == null ? '' : '￥' + item.price): '￥' + item.discountPrice,
                      style: TextStyle(color: item.discountPrice == null ?  Color.fromRGBO(25, 25, 25, 0.8) : Color.fromRGBO(253, 150, 128, 1) , fontSize: 15.0, fontWeight: FontWeight.bold),
                    ),
                    Padding(padding: EdgeInsets.only(left: 10.0),),
                    Text(item.discountPrice == null ? '' : (item.price == null ? '' : '￥' + item.price) ,
                      style: TextStyle(decoration: TextDecoration.lineThrough, color: Color.fromRGBO(216, 216, 216, 1), fontSize: 15.0),
                    )
                  ],
                ),
              ],
            ),
          ],
        ),
      )
    );
  }
}